﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;

    using Csla;

    using MarketPulse.BusinessLogic.Core;

#endregion

namespace MarketPulse.BusinessLogic.Private
{
    /// <summary>
    /// This class represents the responses given to survey questions that were provided when a mystery
    /// shopper or exit interviewer conducts either a mystery shop or exit interview.  Repsonses are stored
    /// as an XML document that maps answer guids in the survey document to actual values reponded with.
    /// Responses have an unique id and also an indentifier that corresponds to the survey and also the 
    /// particular version of that survey.
    /// </summary>
    [Serializable]
    [Csla.Server.ObjectFactory("MarketPulse.DataAccess.Factory.ResponseFactory, MarketPulse.DataAccess.Factory")]
    public class Response : MarketPulseBase<Response>
    {

        #region Member Variables

            /// <summary>
            /// The XML document that describes the reponses given for a particular survey version.  
            /// Sadly, the property type is required to be a string because XElement is not marked
            /// [Serializable] and therefore cannot be used with the Csla framework.
            /// </summary>
            public static PropertyInfo<string> mResponseXMLProperty = RegisterProperty<string>(p => p.ResponseXML);

            /// <summary>
            /// Flag to indicate if the Xml property has been modified
            /// </summary>
            private bool mIsXmlDirty = false;

        #endregion

        #region Constructors

            /// <summary>
            /// Default constructor is private in order to (try to) enforce construction
            /// only via the static factory methods provided.
            /// </summary>
            private Response()
            {
            }

        #endregion

        #region Methods

            #region Factory Methods

                /// <summary>
                /// Creates a new Response via the DataPortal client proxy.
                /// </summary>
                /// <returns>The newly created response</returns>
                public static Response New()
                {
                    return DataPortal.Create<Response>();
                }

                /// <summary>
                /// Gets the specified Response via the DataPortal client proxy 
                /// </summary>
                /// <param name="guid">The Guid of the Respone to retrieve</param>
                /// <returns>A valid Response object (populated or empty)</returns>
                public static Response Get(Guid guid)
                {
                    return DataPortal.Fetch<Response>(new SingleCriteria<Guid>(guid));
                }

                /// <summary>
                /// Gets the specified Response via the DataPortal client proxy 
                /// </summary>
                /// <param name="guid">The Guid of the Respone to retrieve</param>
                /// <returns>A valid Response object (populated or empty)</returns>
                public static Response GetByGuid(Guid guid)
                {
                    return DataPortal.Fetch<Response>(new SingleCriteria<Guid>(guid));
                }

            #endregion

        #endregion

        #region Properties

            /// <summary>
            /// The XML document that describes the reponses given for a particular survey version.  
            /// Sadly, the property type is required to be a string because XElement is not marked
            /// [Serializable] and therefore cannot be used with the Csla framework.
            /// </summary>
            public System.Xml.Linq.XElement ResponseXML
            {
                get { return System.Xml.Linq.XElement.Parse(GetProperty(mResponseXMLProperty)); }
                set
                {
                    mIsXmlDirty = true;
                    SetProperty(mResponseXMLProperty, value.ToString(SaveOptions.DisableFormatting));
                }
            }

            /// <summary>
            /// Flag to indicate if the Xml property has been modified
            /// </summary>
            public bool IsXmlDirty
            {
                get { return mIsXmlDirty; }
            }

        #endregion

    }
}
